﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WithRefsTest.DataModels;

namespace WithRefsTest.ServiceRefTest
{
	[TestClass]
	public class MySvcTest
	{
		private static readonly MySvc.MyServiceClient _proxy = new MySvc.MyServiceClient();

		[TestMethod]
		public void TestGetEntity1()
		{
			Assert.IsNotNull(_proxy);
			
			Entity1 entity = null;
			const int id = 1;
			var callback = new AsyncCallback(res =>
			                                 	{
			                                 		var resp = _proxy.EndGetEntity1(res);
			                                 		entity = resp.GetEntity1Result;
			                                 	});
			_proxy.BeginGetEntity1(new MySvc.GetEntity1Request(id), callback, this);

			Wait(ref entity, 4);
			
			Assert.IsNotNull(entity);
			Assert.AreEqual(entity.Id, id);
		}

		private static void Wait<T>(ref T result, int seconds)
		{
			for (var i = 0; i <= seconds || Equals(result, default(T)); i++)
			{
				System.Threading.Thread.Sleep(1000);
			}
		}
	}
}
